//
//  MainTabBar.swift
//  SinaWebo
//
//  Created by apple on 16/6/7.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

   
    
    private struct mainTabBarInfo {
        static let NameClass: AnyClass = NSClassFromString("UITabBarButton")!
        static let imageName = "tabbar_compose_icon_add"
        static let backImageName = "tabbar_compose_button"
        static let compnent = "_highlighted"
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let mainWidth = bounds.width / 5
        let mainHeight = bounds.height
        let mainRect: CGRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: mainWidth, height: mainHeight))
        var index = 0
        for ids in subviews {
            if ids.isKindOfClass(mainTabBarInfo.NameClass) {
            
              //  mainRect.origin.x = CGFloat(index) * mainWidth
                ids.frame = CGRectOffset(mainRect, CGFloat(index) * mainWidth, 0)
              
              //  print("\(ids.classForCoder)")
                index += (index == 1 ? 2 : 1)
            }
        }
        
      //  let btnFrame = CGRect(origin: CGPoint(x: 2 * mainWidth, y: -2), size: CGSize(width: mainWidth, height: mainHeight))
        addButtonItem.frame = CGRectOffset(mainRect, 2 * mainWidth, -10)
        
    }

    
     lazy var addButtonItem: UIButton = { [unowned self] in return self.addButton() }()
    
    private func addButton() ->UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: mainTabBarInfo.imageName), forState: .Normal)
        button.setImage(UIImage(named: mainTabBarInfo.imageName
            + mainTabBarInfo.compnent), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: mainTabBarInfo.backImageName
            + mainTabBarInfo.compnent), forState: .Normal)
        button.setBackgroundImage(UIImage(named: mainTabBarInfo.backImageName
            + mainTabBarInfo.compnent), forState: .Highlighted)
        addSubview(button)
        return button
    }

   }

